package com.zb.excel.export.controller;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.excel.export.service.DataExportService;
import com.zb.result.Result;
import com.zb.utils.TokenUtil;
import com.zb.vo.CodeListSelectVo;
import com.zb.vo.GoodsSelectVo;
import com.zb.vo.UserSelectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.regex.Pattern;

/**
 * @Author WEN
 * @Date 2023/9/9 0009
 **/
@Api(tags = "数据导出类接口")
@RestController
@RequestMapping("/dataExport")
public class DataExportController {
    @Resource
    private DataExportService dataExportService;

    @ApiOperation("编码批次导出Excel")
    @GetMapping("/codeBatchExportExcel/{batchId}")
    public void codeBatchExportExcel(HttpServletResponse response,
                                @PathVariable @ApiParam(value = "编码批次ID", required = true) Long batchId,
                                @RequestParam @ApiParam(value = "Excel后缀文件类型：1-xlsx，2-xls，3-csv") Integer excelType) {
        Assert.isTrue(Pattern.matches("^(1|2|3)$", excelType.toString()), "Excel后缀文件类型参数不合法");
        dataExportService.codeBatchExportExcel(response, batchId, 1 == excelType? "xlsx" : 2 == excelType? "xls" : "csv");
    }

    @ApiOperation("编码批次导出文本Txt")
    @GetMapping("/codeBatchExportTxt/{batchId}")
    public void codeBatchExportTxt(HttpServletResponse response,
                                   @PathVariable @ApiParam(value = "编码批次ID", required = true) Long batchId) {
        dataExportService.codeBatchExportTxt(response, batchId);
    }

    @ApiOperation("客户导出Excel")
    @PostMapping("/userExportExcel")
    public void userExportExcel(HttpServletRequest request, HttpServletResponse response, @RequestBody @Valid UserSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        dataExportService.userExportExcel(response, custAccId, selectVo);
    }


    @ApiOperation("商品导出Excel")
    @PostMapping("/goodsExportExcel/{type}")
    public void goodsExportExcel(HttpServletRequest request, HttpServletResponse response,
                                 @PathVariable @ApiParam(value = "类型：1-成品，2-配件", required = true) Integer type,
                                 @RequestBody @Valid GoodsSelectVo selectVo){
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        dataExportService.goodsExportExcel(response, custAccId, type, selectVo);
    }

    @ApiOperation("一物一码导出Excel")
    @PostMapping("/codeExportExcel")
    public void codeExportExcel(HttpServletRequest request, HttpServletResponse response, @RequestBody CodeListSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        dataExportService.codeExportExcel(response, custAccId, selectVo);
    }




}
